import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'author_logic.dart';
import 'widget/author_widget.dart';
import '../../routes/routes.dart';
import '../../shared/shared.dart';
import '../../widgets/widgets.dart';

class LoginPage extends GetView<AuthorLogic> {
  const LoginPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Stack(
      children: [
        const Background(imageURL: AssetsURL.backgroundPicture1, opacity: 0.59),
        SafeArea(
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.transparent,
            body: _buildBody(context),
          ),
        )
      ],
    );
  }

  Widget _buildBody(BuildContext context) {
    return Form(
      key: controller.loginFormKey,
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const AuthorBack(),
            _buildAvatar(context),
            CommonWidget.rowHeight(height: 20),
            _buildUser(),
            CommonWidget.rowHeight(height: 60),
            _buildPassword(),
            CommonWidget.rowHeight(height: 20),
            _buildLogin(context),
            _buildRegister(),
            const Text(
              '用其他方式登录',
              style: TextStyle(color: ColorConstants.themeColor, fontSize: 14),
            )
          ],
        ),
      ),
    );
  }

  InputField _buildPassword() {
    return InputField(
        hintText: '请输入密码',
        controller: controller.loginPasswordController,
        keyboardType: TextInputType.emailAddress,
        password: true,
        validator: (value) {
          if (value!.isEmpty) {
            return '请输入密码.';
          }
          if (value.length < 6 || value.length > 15) {
            return '密码长度应该 6~15';
          }
          return null;
        });
  }

  InputField _buildUser() {
    return InputField(
        hintText: '请输入用户名',
        controller: controller.loginUserController,
        keyboardType: TextInputType.text);
  }

  Container _buildRegister() {
    return Container(
        margin: const EdgeInsets.symmetric(vertical: 25),
        child: BlindButton(
            onTap: () => Get.toNamed(
                Routes.author + Routes.gender + Routes.register,
                arguments: controller),
            width: 225,
            height: 50,
            text: '注册',
            fontSize: 20,
            color: Colors.transparent));
  }

  Container _buildLogin(BuildContext context) {
    return Container(
        margin: const EdgeInsets.only(top: 50),
        child: BlindButton(
            onTap: () => controller.login(context),
            width: 225,
            height: 50,
            text: '登录',
            fontSize: 20,
            color: Colors.white.withOpacity(0.33)));
  }

  Widget _buildAvatar(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 20),
      width: 98,
      height: 98,
      decoration: BoxDecoration(
        color: Colors.transparent,
        borderRadius: const BorderRadius.all(Radius.circular(23)),
        border: Border.all(color: ColorConstants.themeColor, width: 2),
        image: DecorationImage(
          image: AssetImage(
              controller.loginUserController.text == controller.userName.value
                  ? AssetsURL.headURL
                  : AssetsURL.defaultHead),
        ),
      ),
    );
  }
}
